import axios from 'axios';

let debug = false;

let Kit = {};
//统一post方法
Kit.post = function (url, query, callback) {
    if (query) {
        axios.post("" + url, query).then(response => {
            if (callback) {
                callback(response.data);
            }
        }).catch(error => {
            let resp = {};
            if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                resp = {
                    status: error.response.status,
                    params: query
                };
            } else {
                // Something happened in setting up the request that triggered an Error
                resp = {
                    message: error.message,
                    params: query
                };
            }
            if (callback) {
                callback(resp);
            } else {
                return resp;
            }
        });
    } else {
        axios.get("" + url).then(response => {
            if (callback) {
                callback(response.data);
            }
        }).catch(error => {
            let resp = {};
            if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                resp = {
                    status: error.response.status,
                    params: query
                };
            } else {
                // Something happened in setting up the request that triggered an Error
                resp = {
                    message: error.message,
                    params: query
                };
            }
            if (callback) {
                callback(resp);
            } else {
                return resp;
            }
        });
    }
}
import webComponent from '@/views/main-components/web.vue';
Kit.adaptMenu = (raw, Main) => {

    let menu = [];
    const rawMenu = raw;
    raw.forEach((item, index) => {
        //主菜单
        if (item.parent_id == 0 && item.type == 'dir') {
            let m = {};
            m.id = item.id;
            m.path = "/" + item.code;
            m.meta = {
                type: 'dir'
            }
            m.name = item.code;
            m.title = item.name;
            m.component = Main;
            m.icon = 'folder';

            function addChildren(parent) {
                parent.children = [];
                // 直接打开链接
                let children = rawMenu.filter(child => child.parent_id == parent.id && (child.link || child.type == 'dir'));
                if (children.length > 0) {
                    children.forEach((child, index) => {
                        let c = {};
                        c.id = child.id;
                        c.path = "/" + child.code;
                        c.name = child.code;
                        c.title = child.name;
                        c.meta = {
                            type: child.type,
                            link: "" + child.link
                        };
                        c.component = webComponent;
                        parent.children.push(c);
                        addChildren(c);
                    })
                }
            }
            addChildren(m);

            menu.push(m);
        } else if (item.parent_id == 0 && item.type == 'diy') {
            let m = {};
            m.id = item.id;
            m.path = "/" + item.code;
            m.meta = {
                type: item.type,
                link: item.link
            }
            m.name = item.code;
            m.title = item.name;
            m.component = Main;
            m.icon = 'folder';
            m.children = [];
            menu.push(m);
        }
    })
    return menu;
}

export default Kit;